<template>
  <div class="box">
    <div>
      <span>创建时间</span>
      <el-date-picker
        v-model="value1"
        type="datetimerange"
        range-separator="To"
        start-placeholder="Start date"
        end-placeholder="End date"
      />
    </div>
    <!-- 搜索 -->
    <div class="mt-4">
      <el-input
        v-model="input3"
        placeholder="Please input"
        class="input-with-select"
      >
        <template #append>
          <el-button :icon="Search" @click="search" />
        </template>
      </el-input>
    </div>
    <!-- 新增 -->
    <el-button type="primary" @click="add">新增</el-button>
  </div>
  <!-- 表格 -->
  <el-table :data="dataList" style="width: 100%">
    <el-table-column fixed prop="id" label="ID" width="180" />
    <el-table-column prop="type" label="车次/类型" width="160" />
    <el-table-column prop="line" label="线路" width="330" />
    <el-table-column prop="time" label="创建时间" width="170" />

    <el-table-column prop="status" label="状态" width="180">
      <template #default="scope">
        <el-switch v-model="scope.row.status"> </el-switch>
      </template>
    </el-table-column>

    <el-table-column label="Operations" width="150">
      <template #default="scope">
        <el-button type="primary" size="small" @click="handleClick(scope.row)"
          >删除</el-button
        >
        <el-button type="primary" size="small">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="foot">
    <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :page-sizes="[10]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="length"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 对话框 -->
  <el-dialog
    v-model="dialogVisible"
    title="Tips"
    width="50%"
    :before-close="handleClose"
  >
    <!-- 表单 -->
    <el-form :model="form" label-width="120px">
      <div class="form">
        <el-form-item label="车次类型">
          <el-input class="inpp" v-model="name" />
        </el-form-item>
        <el-form-item label="车次">
          <el-input class="inpp" v-model="car" />
        </el-form-item>
      </div>
    </el-form>

    <div>车站线路：{{ str }}</div>
    <!-- 穿梭 -->
    <el-transfer
      id="trans"
      v-model="value"
      :data="data"
      @change="changeStation"
    />
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref } from "vue";
import axios from "@/utils/axios";
import { ElMessage } from "element-plus";
import { ElMessageBox } from "element-plus";
import { Search } from "@element-plus/icons-vue";
const ExportJsonExcel = require("js-export-excel");
const tableData = ref();
const name = ref("高铁");
const car = ref("");
const value1 = ref([]);
const input3 = ref("");
const currentPage4 = ref(1);
const pageSize4 = ref(10);
const small = ref(false);
const length = ref();
const dataList = ref();
const carData = ref(["北京", "天津", "拉萨", "泰来", "纽约", "伦敦"]);
const str = ref("");

const pagess = ref("1");
console.log(pagess);
// 线
const line = ref([]);

const dialogVisible = ref(false);


// 渲染传穿梭的数据
const generateData = () => {
  const data = [];
  for (let i = 0; i < carData.value.length; i++) {
    data.push({
      key: i,
      label: carData.value[i],
    });
  }
  return data;
};

const data = ref(generateData());
const value = ref([]);


// 关闭对话框
const handleClose = (done) => {
  ElMessageBox.confirm("确定关闭对话框吗")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
};

// 删除
const handleClick = (row) => {
  console.log(row);
  const { id } = row;
  const index = dataList.value.findIndex((item) => item.id === id);
  dataList.value.splice(index, 1);
  ElMessage({
    message: "删除成功",
    type: "success",
  });
};

// 添加
const add = () => {
  dialogVisible.value = true;
  car.value = "";
};

// 点击页码
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`);
  pagess.value = val;
  axios.get("/getList").then((res) => {
    dataList.value = res.data.data.slice((val - 1) * 10, val * 10);
  });
};


// 获取渲染数据
onMounted(() => {
  axios.get("/getList").then((res) => {
    tableData.value = res.data.data;
    dataList.value = tableData.value.slice(0, 10);
    length.value = res.data.data.length;
  });
});

// 搜索
const search = () => {
  console.log(input3.value);
  const arr = [];
  tableData.value.filter((item) => {
    if (item.type.includes(input3.value)) {
      return arr.push(item);
    }
  });

  dataList.value = arr;
  ElMessage({
    message: "搜索成功",
    type: "success",
  });
};

// 初始化穿梭树
function init() {
  let brr;
  for (let i = 0; i < line.value.length; i++) {
    brr += line.value[i] + "→"
    
  }
  brr = brr.split("ed")[1]
  brr = brr.substring(0,brr.lastIndexOf(("→")))
  return brr;
}
// 改变线路
const changeStation = (val) => {
  const arr = [];
  val.map((item, index) => {
    return arr.push(carData.value[item]);
  });
  // console.log(arr);
  line.value = arr;
  // 线路
  str.value = init();
  // console.log(line.value, init());
};

// 点击确定  新增
const confirm = () => {
  dialogVisible.value = false;
  // console.log(name.value,car.value,str.value);
  const arr = JSON.parse(JSON.stringify(tableData.value));
  arr.unshift({
    id: tableData.value.length + 1,
    type: name.value + "/" + car.value,
    line: str.value,
    time: "2023-11-11 13:13:13",
    status: true,
  });
  console.log(arr);
  tableData.value = arr;
  length.value = tableData.value.length;
  dataList.value = tableData.value.slice(
    (pagess.value - 1) * 10,
    pagess.value * 10
  );
  ElMessage({
    message: "添加成功",
    type: "success",
  });
};
</script>

<style scoped>
.el-range-editor.el-input__wrapper {
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  width: 200px;
}

.box {
  width: 100%;
  height: 50px;
  background: white;
  display: flex;
  justify-content: space-between;
}

.foot {
  width: 100%;
  height: 50px;
  background: white;
  display: flex;
  align-items: center;
  flex-direction: row-reverse;
}
.inpp {
  width: 100px;
}
.form {
  display: flex;
}
</style>
